<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\decorate\model\AdminColumn as column_model;

class AdminColumn extends AdminRest{
    protected $model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new column_model();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:栏目列表
     */
    public function columnList(){
        $input = $this->_input;
        if(!empty($input['title'])){
            $dis[] = ['title','like',"%\\".$input['title']."%"];
        }
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data  = $this->model->columnList($dis,$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:添加栏目
     */
    public function columnAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $this->checkData($input);
        $res   = $this->model->columnAdd($input);
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:编辑|回显栏目
     */
    public function columnUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            $this->checkData($input);
            $data = $this->model->columnUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->columnInfo(['id'=>$input['id']]);
        }
        return $this->success($data);
    }


    /**
     * User: chenniang
     * Date: 2019-10-14 18:42
     * @return void
     * descption:栏目类型
     */
    public function columnType(){
        $input = $this->_input;
        $id    = !empty($input['id'])?$input['id']:'';
        $type  = $this->model->columnType($this->_uniacid,$id);
        return $this->success($type);
    }


    /**
     * User: chenniang
     * Date: 2019-10-14 18:31
     * @return void
     * descption:检查是否重复添加
     */
    public function checkData($input){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['type','=',$input['type']];
        if(!empty($input['id'])){
            $dis[] = ['id','<>',$input['id']];
        }
        $data = $this->model->columnInfo($dis);
        if(!empty($data)){
            $this->errorMsg('This type of data already exists');
        }
        return true;

    }








}
